package com.example.shaoying.yuejian.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;

import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.example.shaoying.yuejian.R;
import com.example.shaoying.yuejian.fragment.clubeFragment.Clube01Fragment;
import com.example.shaoying.yuejian.fragment.clubeFragment.Clube02Fragment;


public class ClubeFragment extends Fragment implements View.OnClickListener {

    private View view;
    private LinearLayout lclube01;
    private LinearLayout lclube02;
    private Fragment clube01Fragment;
    private Fragment clube02Fragment;
    private LinearLayout Bclube1;
    private LinearLayout Bclube2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.tab02, container, false);
        initView();
        initEvent();
        setSelect(0);
        return view;
    }
    private void initEvent()
    {
        Bclube1.setOnClickListener( this);
        Bclube2.setOnClickListener(this);
    }


    private void initView()
    {
        lclube01 = (LinearLayout)view.findViewById(R.id.renqi);
        lclube02 = (LinearLayout)view.findViewById(R.id.zuijin);

        Bclube1 = (LinearLayout)view.findViewById(R.id.renqi);
        Bclube2 = (LinearLayout)view.findViewById(R.id.zuijin);

    }



    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.renqi:
                setSelect(0);
                break;
            case R.id.zuijin:
                setSelect(1);
                break;
            default:
                break;
        }
    }

    private void setSelect(int i)
    {
        FragmentManager fm =getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (clube01Fragment == null)
                {
                    clube01Fragment = new Clube01Fragment();
                    transaction.add(R.id.id_tab02, clube01Fragment);

                } else
                {
                    transaction.show(clube01Fragment);
                }
                lclube02.setBackgroundColor(Color.parseColor("#000000"));
                lclube01.setBackgroundColor(Color.parseColor("#00cc33"));
                break;
            case 1:
                if (clube02Fragment == null)
                {
                    clube02Fragment = new Clube02Fragment();
                    transaction.add(R.id.id_tab02, clube02Fragment);
                } else
                {
                    transaction.show(clube02Fragment);

                }
                lclube01.setBackgroundColor(Color.parseColor("#000000"));
                lclube02.setBackgroundColor(Color.parseColor("#00cc33"));
                break;

            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (clube01Fragment != null)
        {
            transaction.hide(clube01Fragment);
        }
        if (clube02Fragment != null)
        {
            transaction.hide(clube02Fragment);
        }

    }

}
